#!/usr/bin/env python  
# encoding: utf-8


""" 
@version: v1.0 
@author: Shylock 
@license: Apache Licence  
@contact: JYFelt@163.com 
@site: http://blog.csdn.net/weixin_38034182 
@software: PyCharm Community Edition 
@file: CInfoPage.py
@time: 2017/12/23 21:57 
"""
from tkinter import *
from tkinter import messagebox as mb
import MYSQL_CL as ms
import MainPage as mp
import os
import time


# 公司信息插入界面
class insertPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量
        self.root.geometry('800x500')
        self.text_1 = StringVar()
        self.text_2 = StringVar()
        self.text_3 = StringVar()
        self.text_4 = StringVar()
        self.text_5 = StringVar()
        self.text_6 = StringVar()
        self.text_7 = StringVar()

        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)
        self.page.pack()
        self.showPage()

    def showPage(self):
        Button(self.page, text='返回', command=self.jumpback).grid(row=10, column=2)
        b2 = Button(self.page, text='插入信息到公司', command=self.show).grid(row=10, column=1)

    def show(self):
        Label(self.page, text='注意').grid(row=0, column=0)
        Label(self.page, text='*号为必填项目').grid(row=0, column=1)
        Label(self.page, text='*请输入公司ID: ').grid(row=2, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_1).grid(row=2, column=1, stick=E)

        Label(self.page, text='请输入公司名称: ').grid(row=3, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_2).grid(row=3, column=1, stick=E)

        Label(self.page, text='请输入工作ID: ').grid(row=4, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_3).grid(row=4, column=1, stick=E)

        Label(self.page, text='请输入工作工资: ').grid(row=5, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_4).grid(row=5, column=1, stick=E)

        Label(self.page, text='请输入需求人数: ').grid(row=6, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_5).grid(row=6, column=1, stick=E)

        Label(self.page, text='请输入录用人数: ').grid(row=7, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_6).grid(row=7, column=1, stick=E)

        Label(self.page, text='请输入工作名称: ').grid(row=8, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_7).grid(row=8, column=1, stick=E)

        Button(self.page, text='插入', command=self.exec_).grid(row=10, column=0)

    def exec_(self):
        list = []
        list.append(self.text_1.get())
        list.append(self.text_2.get())
        list.append(self.text_3.get())
        list.append(self.text_4.get())
        list.append(self.text_5.get())
        list.append(self.text_6.get())
        list.append(self.text_7.get())

        __sql_1 = "SELECT 公司ID FROM 公司信息"

        t = (ms.exec(__sql_1))

        __sql = "INSERT INTO 公司信息(公司ID,公司名称,工作ID,工作工资,需求人数,录用人数,工作名称" \
                ") VALUES('%s','%s','%s','%s','%s','%s','%s')" % (
                    list[0], list[1], list[2], list[3], list[4], list[5], list[6])
        for i in range(len(t)):
            if self.text_1.get() not in (t[i]):
                print(ms.update(sql=__sql))
                ms.update(sql=__sql)
                Label(self.page, text='插入成功').grid(row=11)
            else:
                mb.showwarning('啊呀，出错啦~', message='插入失败,ID重复')

    def jumpback(self):
        self.page.destroy()
        mp.MainPage(self.root)


# 查询界面 over
class searchPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量
        self.root.geometry('800x500')
        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)
        self.page.pack()
        self.show = self.showPage()

    def showPage(self):

        Button(self.page, text='查询所有公司信息', command=self.gongsi).grid(row=0, column=0)
        Button(self.page, text='返回', command=self.jumpback).grid(row=0, column=5)

    def gongsi(self):
        t = ms.exec(sql='SELECT * FROM 公司信息')
        text = ('公司ID', '公司名称', '工作ID', '工作工资', '需求人数', '录用人数', '工作名称')

        for i in range(len(text)):
            a = text[i]
            Label(self.page, text=a).grid(row=1, column=i)

        for i in range(len(t)):
            for j in range(len(t[i])):
                a = t[i][j]
                Label(self.page, text='%s' % str(a)).grid(row=i + 2, column=j)

    def jumpback(self):
        self.page.destroy()
        mp.MainPage(self.root)


# 修改、删除界面
class modifyPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量
        self.root.geometry('800x500')
        self.text_1 = StringVar()
        self.text_2 = StringVar()
        self.text_3 = StringVar()
        self.text_4 = StringVar()
        self.text_5 = StringVar()
        self.text_6 = StringVar()
        self.text_7 = StringVar()
        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)
        self.page.pack()
        self.show = self.showPage()

    def showPage(self):
        Button(self.page, text='返回', command=self.jumpback).grid(row=0, column=0)
        Button(self.page, text='修改信息', command=self.modifypage).grid(row=0, column=1)
        Button(self.page, text='删除信息', command=self.deletepage).grid(row=0, column=2)

    def deletepage(self):

        Label(self.page, text='*请输入要删除的公司ID: ').grid(row=2, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_1).grid(row=2, column=1, stick=E)

        Button(self.page, text='删除', command=self.deletePage).grid(row=3, column=2)

    def deletePage(self):
        self.t = self.text_1.get()

        self.__sql_1 = "SELECT 公司ID FROM 公司信息"
        self.__sql = "delete from 公司信息 where 公司ID='%s'" % (self.t)
        print('111')
        for i in range(len(ms.exec(self.__sql_1))):
            if self.t in (ms.exec(self.__sql_1)[i]):
                Label(self.page, text='删除中').grid(row=3)

                ms.update(self.__sql)
                print('111')
                if self.t not in (ms.exec(self.__sql_1)):

                    mb.showwarning('成功！', message='删除成功')
                    break
                else:
                    mb.showwarning('啊呀，出错啦~', message='删除失败，请重试')
                    break

            else:
                mb.showwarning('啊呀，出错啦~', message='删除错误，公司ID不存在')
                break

    def modifypage(self):
        pass

    def jumpback(self):
        self.page.destroy()
        mp.MainPage(self.root)


# 关于界面
class aboutPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量
        self.root.geometry('800x500')  # 设置窗口大小
        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)
        self.page.pack()
        self.show = self.showPage()

    def showPage(self):
        t = ms.exec(sql='SELECT VERSION()')

        Message1 = "职业介绍信息管理系统"
        Message2 = "v 0.23.1"
        Message3 = "@Author: Shylock 小仲马"
        Message4 = '当前连接数据库版本' + str(t)
        Message5 = '返回'
        Label(self.page, text=Message1).grid(row=5)
        Label(self.page, text=Message2).grid(row=6)
        Label(self.page, text=Message3).grid(row=7)
        Label(self.page, text=Message4).grid(row=8)
        Button(self.page, text=Message5, command=self.jumpback).grid(row=9, column=1)

    def jumpback(self):
        self.page.destroy()
        mp.MainPage(self.root)
